home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / MooseX / Getopt.pm next >
Encoding:
Perl POD Document  |  2010-07-07  |  7.9 KB  |  308 lines

  1. package MooseX::Getopt;
  2. BEGIN {
  3.   $MooseX::Getopt::AUTHORITY = 'cpan:STEVAN';
  4. }
  5. BEGIN {
  6.   $MooseX::Getopt::VERSION = '0.31';
  7. }
  8. # ABSTRACT: A Moose role for processing command line options
  9.  
  10. use Moose::Role 0.56;
  11.  
  12. with 'MooseX::Getopt::GLD';
  13.  
  14. no Moose::Role;
  15.  
  16. 1;
  17.  
  18.  
  19. __END__
  20. =pod
  21.  
  22. =encoding utf-8
  23.  
  24. =head1 NAME
  25.  
  26. MooseX::Getopt - A Moose role for processing command line options
  27.  
  28. =head1 SYNOPSIS
  29.  
  30.   ## In your class
  31.   package My::App;
  32.   use Moose;
  33.  
  34.   with 'MooseX::Getopt';
  35.  
  36.   has 'out' => (is => 'rw', isa => 'Str', required => 1);
  37.   has 'in'  => (is => 'rw', isa => 'Str', required => 1);
  38.  
  39.   # ... rest of the class here
  40.  
  41.   ## in your script
  42.   #!/usr/bin/perl
  43.  
  44.   use My::App;
  45.  
  46.   my $app = My::App->new_with_options();
  47.   # ... rest of the script here
  48.  
  49.   ## on the command line
  50.   % perl my_app_script.pl -in file.input -out file.dump
  51.  
  52. =head1 DESCRIPTION
  53.  
  54. This is a role which provides an alternate constructor for creating
  55. objects using parameters passed in from the command line.
  56.  
  57. This module attempts to DWIM as much as possible with the command line
  58. params by introspecting your class's attributes. It will use the name
  59. of your attribute as the command line option, and if there is a type
  60. constraint defined, it will configure Getopt::Long to handle the option
  61. accordingly.
  62.  
  63. You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait> or the
  64. attribute metaclass L<MooseX::Getopt::Meta::Attribute> to get non-default
  65. commandline option names and aliases.
  66.  
  67. You can use the trait L<MooseX::Getopt::Meta::Attribute::Trait::NoGetopt>
  68. or the attribute metaclass L<MooseX::Getopt::Meta::Attribute::NoGetopt>
  69. to have C<MooseX::Getopt> ignore your attribute in the commandline options.
  70.  
  71. By default, attributes which start with an underscore are not given
  72. commandline argument support, unless the attribute's metaclass is set
  73. to L<MooseX::Getopt::Meta::Attribute>. If you don't want your accessors
  74. to have the leading underscore in their name, you can do this:
  75.  
  76.   # for read/write attributes
  77.   has '_foo' => (accessor => 'foo', ...);
  78.  
  79.   # or for read-only attributes
  80.   has '_bar' => (reader => 'bar', ...);
  81.  
  82. This will mean that Getopt will not handle a --foo param, but your
  83. code can still call the C<foo> method.
  84.  
  85. If your class also uses a configfile-loading role based on
  86. L<MooseX::ConfigFromFile>, such as L<MooseX::SimpleConfig>,
  87. L<MooseX::Getopt>'s C<new_with_options> will load the configfile
  88. specified by the C<--configfile> option (or the default you've
  89. given for the configfile attribute) for you.
  90.  
  91. Options specified in multiple places follow the following
  92. precedence order: commandline overrides configfile, which
  93. overrides explicit new_with_options parameters.
  94.  
  95. =head2 Supported Type Constraints
  96.  
  97. =over 4
  98.  
  99. =item I<Bool>
  100.  
  101. A I<Bool> type constraint is set up as a boolean option with
  102. Getopt::Long. So that this attribute description:
  103.  
  104.   has 'verbose' => (is => 'rw', isa => 'Bool');
  105.  
  106. would translate into C<verbose!> as a Getopt::Long option descriptor,
  107. which would enable the following command line options:
  108.  
  109.   % my_script.pl --verbose
  110.   % my_script.pl --noverbose
  111.  
  112. =item I<Int>, I<Float>, I<Str>
  113.  
  114. These type constraints are set up as properly typed options with
  115. Getopt::Long, using the C<=i>, C<=f> and C<=s> modifiers as appropriate.
  116.  
  117. =item I<ArrayRef>
  118.  
  119. An I<ArrayRef> type constraint is set up as a multiple value option
  120. in Getopt::Long. So that this attribute description:
  121.  
  122.   has 'include' => (
  123.       is      => 'rw',
  124.       isa     => 'ArrayRef',
  125.       default => sub { [] }
  126.   );
  127.  
  128. would translate into C<includes=s@> as a Getopt::Long option descriptor,
  129. which would enable the following command line options:
  130.  
  131.   % my_script.pl --include /usr/lib --include /usr/local/lib
  132.  
  133. =item I<HashRef>
  134.  
  135. A I<HashRef> type constraint is set up as a hash value option
  136. in Getopt::Long. So that this attribute description:
  137.  
  138.   has 'define' => (
  139.       is      => 'rw',
  140.       isa     => 'HashRef',
  141.       default => sub { {} }
  142.   );
  143.  
  144. would translate into C<define=s%> as a Getopt::Long option descriptor,
  145. which would enable the following command line options:
  146.  
  147.   % my_script.pl --define os=linux --define vendor=debian
  148.  
  149. =back
  150.  
  151. =head2 Custom Type Constraints
  152.  
  153. It is possible to create custom type constraint to option spec
  154. mappings if you need them. The process is fairly simple (but a
  155. little verbose maybe). First you create a custom subtype, like
  156. so:
  157.  
  158.   subtype 'ArrayOfInts'
  159.       => as 'ArrayRef'
  160.       => where { scalar (grep { looks_like_number($_) } @$_)  };
  161.  
  162. Then you register the mapping, like so:
  163.  
  164.   MooseX::Getopt::OptionTypeMap->add_option_type_to_map(
  165.       'ArrayOfInts' => '=i@'
  166.   );
  167.  
  168. Now any attribute declarations using this type constraint will
  169. get the custom option spec. So that, this:
  170.  
  171.   has 'nums' => (
  172.       is      => 'ro',
  173.       isa     => 'ArrayOfInts',
  174.       default => sub { [0] }
  175.   );
  176.  
  177. Will translate to the following on the command line:
  178.  
  179.   % my_script.pl --nums 5 --nums 88 --nums 199
  180.  
  181. This example is fairly trivial, but more complex validations are
  182. easily possible with a little creativity. The trick is balancing
  183. the type constraint validations with the Getopt::Long validations.
  184.  
  185. Better examples are certainly welcome :)
  186.  
  187. =head2 Inferred Type Constraints
  188.  
  189. If you define a custom subtype which is a subtype of one of the
  190. standard L</Supported Type Constraints> above, and do not explicitly
  191. provide custom support as in L</Custom Type Constraints> above,
  192. MooseX::Getopt will treat it like the parent type for Getopt
  193. purposes.
  194.  
  195. For example, if you had the same custom C<ArrayOfInts> subtype
  196. from the examples above, but did not add a new custom option
  197. type for it to the C<OptionTypeMap>, it would be treated just
  198. like a normal C<ArrayRef> type for Getopt purposes (that is,
  199. C<=s@>).
  200.  
  201. =head1 METHODS
  202.  
  203. =head2 B<new_with_options (%params)>
  204.  
  205. This method will take a set of default C<%params> and then collect
  206. params from the command line (possibly overriding those in C<%params>)
  207. and then return a newly constructed object.
  208.  
  209. The special parameter C<argv>, if specified should point to an array
  210. reference with an array to use instead of C<@ARGV>.
  211.  
  212. If L<Getopt::Long/GetOptions> fails (due to invalid arguments),
  213. C<new_with_options> will throw an exception.
  214.  
  215. If L<Getopt::Long::Descriptive> is installed and any of the following
  216. command line params are passed, the program will exit with usage
  217. information (and the option's state will be stored in the help_flag
  218. attribute). You can add descriptions for each option by including a
  219. B<documentation> option for each attribute to document.
  220.  
  221.   --?
  222.   --help
  223.   --usage
  224.  
  225. If you have L<Getopt::Long::Descriptive> the C<usage> param is also passed to
  226. C<new> as the usage option.
  227.  
  228. =head2 B<ARGV>
  229.  
  230. This accessor contains a reference to a copy of the C<@ARGV> array
  231. as it originally existed at the time of C<new_with_options>.
  232.  
  233. =head2 B<extra_argv>
  234.  
  235. This accessor contains an arrayref of leftover C<@ARGV> elements that
  236. L<Getopt::Long> did not parse.  Note that the real C<@ARGV> is left
  237. un-mangled.
  238.  
  239. =head2 B<usage>
  240.  
  241. This accessor contains the L<Getopt::Long::Descriptive::Usage> object (if
  242. L<Getopt::Long::Descriptive> is used).
  243.  
  244. =head2 B<help_flag>
  245.  
  246. This accessor contains the boolean state of the --help, --usage and --?
  247. options (true if any of these options were passed on the command line).
  248.  
  249. =head2 B<meta>
  250.  
  251. This returns the role meta object.
  252.  
  253. =head1 AUTHORS
  254.  
  255. =over 4
  256.  
  257. =item *
  258.  
  259. Stevan Little <stevan@iinteractive.com>
  260.  
  261. =item *
  262.  
  263. Brandon L. Black <blblack@gmail.com>
  264.  
  265. =item *
  266.  
  267. Yuval Kogman <nothingmuch@woobling.org>
  268.  
  269. =item *
  270.  
  271. Ryan D Johnson <ryan@innerfence.com>
  272.  
  273. =item *
  274.  
  275. Drew Taylor <drew@drewtaylor.com>
  276.  
  277. =item *
  278.  
  279. Tomas Doran <bobtfish@bobtfish.net>
  280.  
  281. =item *
  282.  
  283. Florian Ragwitz <rafl@debian.org>
  284.  
  285. =item *
  286.  
  287. Dagfinn Ilmari Manns├Ñker <ilmari@ilmari.org>
  288.  
  289. =item *
  290.  
  291. Avar Arnfj├╢r├░ Bjarmason <avar@cpan.org>
  292.  
  293. =item *
  294.  
  295. Chris Prather <perigrin@cpan.org>
  296.  
  297. =back
  298.  
  299. =head1 COPYRIGHT AND LICENSE
  300.  
  301. This software is copyright (c) 2010 by Infinity Interactive, Inc.
  302.  
  303. This is free software; you can redistribute it and/or modify it under
  304. the same terms as the Perl 5 programming language system itself.
  305.  
  306. =cut
  307.  
  308.